class UserAdminController < ApplicationController

#	before_filter :login_required, :except=> [ :index, :list]
	before_filter :authorize, :except => :login
	
	layout "board"
  
	def add_tag
  end

  def create_tag
  end

  def delete_tag
  end

  def edit_tag
  end

  def update_tag
  end

	def add_domain
		table = params[:id]
		@domain = Domain.new
		@domain.table_id = table
	end

	def create_domain
		@domain = Domain.new(params[:domain])
		@domain.hit = 0
		@domain.fullDomain = @domain.fullDomain.strip #removing white space
		uri = URI.parse(@domain.fullDomain)
		if !uri.scheme # pyrenees.com => http://pyrenees.com
			@domain.fullDomain.insert(0,'http://')
		end
		
		@domain.scheme = uri.scheme
		@domain.userinfo = uri.userinfo 
		@domain.host = uri.host
		@domain.port = uri.port 
		@domain.registry = uri.registry
		@domain.path = uri.path
		@domain.opaque = uri.opaque
		@domain.query = uri.query 
		@domain.fragment = uri.fragment
		
		table = Table.find(@domain.table)
		panel = Panel.find(table.panel_id)

		user = User.find(panel.user)
		
		Domain.transaction do
			@domain.save!		
		end
		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end

	def edit_domain
		@domain = Domain.find(params[:id])

	end

	def update_domain
		@domain = Domain.find(params[:id])
		table = Table.find(@domain.table_id)
		panel = Panel.find(table.panel_id)
		user = User.find(panel.user_id)

		@domain.update_attributes(params[:domain])
		flash[:notice] = 'Domain was successfully updated.'
		redirect_to(:controller => :board, :action => :index, :id => user.userName) 
	end

	def delete_domain
		@domain = Domain.find(params[:id])
		table = Table.find(@domain.table_id)
		panel = Panel.find(table.panel_id)
		user = User.find(panel.user_id)
		if (session[:user_id] == user)
			@domain.destroy
		end

		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end
	
	def add_row
	  table = Table.find(params[:id])
	  table.row = table.row + 1
	  table.save
	  user = table.panel.user	  
	  redirect_to(:controller => :board, :action => :index, :id => user.userName) 	  
	end
	
	def delete_row
	  table = Table.find(params[:id])
	  table.row = table.row - 1
	  table.save
	  user = table.panel.user	  
	  redirect_to(:controller => :board, :action => :index, :id => user.userName) 	  
  end
	def edit_table
		@table = Table.find(params[:id])
	end

	def update_table
		@table = Table.find(params[:id])
		user = User.find(@table.panel.user)
		@table.update_attributes(params[:table])
		flash[:notice] = 'Table was successfully updated.'
		redirect_to(:controller => :board, :action => :index, :id => user.userName) 
	end


	def delete_table
		@table = Table.find(params[:id])
		user = User.find(@table.panel.user)

		@table.destroy

		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end

	def add_table
		panel = params[:id]
		@table = Table.new
		@table.panel_id = panel
	end

	def create_table
		@table = Table.new(params[:table])

		panel = Panel.find(@table.panel_id)
		user = User.find(panel.user_id)
		
		Table.transaction do
			@table.save!		
		end
		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end

	def add_panel
		user = params[:id]
		@panel = Panel.new
		@panel.user_id = user
	end

	def create_panel
		@panel = Panel.new(params[:panel])
		user = User.find(@panel.user_id)

		Table.transaction do
			@panel.save!		
		end
		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end

	def edit_panel
		@panel = Panel.find(params[:id])
	end

	def update_panel
		@panel = Panel.find(params[:id])
		user = User.find(@panel.user)

		@panel.update_attributes(params[:panel])
		flash[:notice] = 'Panel was successfully updated.'
		redirect_to(:controller => :board, :action => :index, :id => user.userName) 
	end

	def delete_panel
		@panel = Panel.find(params[:id])
		user = User.find(@panel.user_id)

		@panel.destroy

		respond_to do |format|
			format.html { redirect_to(:controller => :board, :action => :index, :id => user.userName) }
			format.xml  { head :ok }
		end
	end
end



